package main

import "github.com/warthog618/go-gpiocdev"

/*
gpiochip0
line  12:     "PIN_15"       unused   input  active-high
        line  13:     "PIN_22"       unused   input  active-high

gpiochip1
        line  36:      "PIN_5"       unused   input  active-high
        line  37:      "PIN_3"       unused   input  active-high
        line  41:      "PIN_8"       unused   input  active-high
        line  42:     "PIN_10"       unused   input  active-high
        line  43:     "PIN_24"       unused   input  active-high
        line  44:     "PIN_23"       unused   input  active-high
        line  45:     "PIN_19"       unused   input  active-high
        line  46:     "PIN_21"       unused   input  active-high
        line 256:     "PIN_26"       unused   input  active-high
        line 257:     "PIN_32"       unused   input  active-high
        line 258:     "PIN_12"       unused  output  active-high
        line 259:     "PIN_36"       unused   input  active-high
        line 260:     "PIN_40"       unused   input  active-high
        line 261:     "PIN_38"       unused   input  active-high
        line 262:     "PIN_33"       unused   input  active-high
        line 263:      "PIN_7"       unused   input  active-high
        line 264:     "PIN_29"       unused   input  active-high
        line 265:     "PIN_11"       unused   input  active-high
        line 266:     "PIN_13"       unused   input  active-high
        line 267:     "PIN_16"       unused   input  active-high
        line 268:     "PIN_31"       unused   input  active-high
        line 269:     "PIN_35"       unused   input  active-high
        line 270:     "PIN_18"       unused   input  active-high
        line 271:     "PIN_28"       unused   input  active-high
        line 272:     "PIN_27"       unused   input  active-high
*/
var (
	pin3  = &Pin{Chip: "gpiochip1", Offset: 37}
	pin5  = &Pin{Chip: "gpiochip1", Offset: 36}
	pin7  = &Pin{Chip: "gpiochip1", Offset: 263}
	pin8  = &Pin{Chip: "gpiochip1", Offset: 41}
	pin10 = &Pin{Chip: "gpiochip1", Offset: 42}
	pin11 = &Pin{Chip: "gpiochip1", Offset: 265}
	pin12 = &Pin{Chip: "gpiochip1", Offset: 258} 
	pin13 = &Pin{Chip: "gpiochip1", Offset: 266}
	pin15 = &Pin{Chip: "gpiochip0", Offset: 12}
	pin16 = &Pin{Chip: "gpiochip1", Offset: 267}
	pin18 = &Pin{Chip: "gpiochip1", Offset: 270}
	pin19 = &Pin{Chip: "gpiochip1", Offset: 45}
	pin21 = &Pin{Chip: "gpiochip1", Offset: 46}
	pin22 = &Pin{Chip: "gpiochip0", Offset: 13}
	pin23 = &Pin{Chip: "gpiochip1", Offset: 44}
	pin24 = &Pin{Chip: "gpiochip1", Offset: 43}
	pin26 = &Pin{Chip: "gpiochip1", Offset: 256}
	pin27 = &Pin{Chip: "gpiochip1", Offset: 272}
	pin28 = &Pin{Chip: "gpiochip1", Offset: 271}
	pin29 = &Pin{Chip: "gpiochip1", Offset: 264}
	pin31 = &Pin{Chip: "gpiochip1", Offset: 268}
	pin32 = &Pin{Chip: "gpiochip1", Offset: 257}
	pin33 = &Pin{Chip: "gpiochip1", Offset: 262}
	pin35 = &Pin{Chip: "gpiochip1", Offset: 269}
	pin36 = &Pin{Chip: "gpiochip1", Offset: 259}
	pin38 = &Pin{Chip: "gpiochip1", Offset: 261}
	pin40 = &Pin{Chip: "gpiochip1", Offset: 260}
)

type Pin struct {
	Chip   string
	Offset int
	l      *gpiocdev.Line
}

func (p *Pin) Close() error {
	if p.l != nil {
		return p.l.Close()
	}
	return nil
}
func (d *Pin) Value() (int, error) {
	return d.l.Value()
}

type DI struct {
	Pin
}

func NewDI(chip string, offset int) (*DI, error) {
	di := new(DI)
	di.Pin = Pin{
		Chip:   chip,
		Offset: offset,
	}
	l, err := gpiocdev.RequestLine(di.Chip, di.Offset, gpiocdev.AsInput)
	if err != nil {
		return nil, err
	}
	di.l = l
	return di, nil
}


type DO struct {
	Pin
}

func NewDO(chip string, offset int, values ...int) (*DO, error) {
	do := new(DO)
	do.Pin = Pin{
		Chip:   chip,
		Offset: offset,
	}
	l, err := gpiocdev.RequestLine(do.Chip, do.Offset, gpiocdev.AsOutput(values...))
	if err != nil {
		return nil, err
	}
	do.l = l
	return do, nil
}

func (d *DO) Set(v int) error {
	return d.l.SetValue(v)
}
